package com.ruoyi.web.wuchuan.command;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.ruoyi.web.wuchuan.dto.PDFProperties;
import com.ruoyi.web.wuchuan.dto.Stzbtj;
import com.ruoyi.web.wuchuan.dto.TzhqytxDTO;
import com.ruoyi.web.wuchuan.enmu.Param;
import com.ruoyi.web.wuchuan.handle.PDFModule;
import com.ruoyi.web.wuchuan.util.PDFutil;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class HomogeneousArea implements PDFModule {

    private static final String pdfName = "作业区域生态适宜报告" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".pdf";
    private static final String tempPath = System.getProperty("java.io.tmpdir");
    private final String zipName = "作业区域生态适宜报告" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".zip";


    @Override
    public String getZipName() {
        return zipName;
    }

    @Override
    public String analyticalDataByType(PDFProperties pdfProperties, String uuid) {
        Document document = new Document(PageSize.A4);
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(tempPath+pdfName));
            document.addTitle("example of PDF");
            document.open();
            document.add(CretePDF(pdfProperties,uuid));
        } catch (FileNotFoundException | DocumentException e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
        return tempPath+pdfName;
    }
    private PdfPTable CretePDF(PDFProperties pdfProperties,String uuid) {
        PdfPTable table = new PdfPTable(Param.tittleSzls);
        table.setSpacingBefore(20f);//设置页边距
        table.setWidthPercentage(100);//设置表格宽度为100%
        PDFutil pdFutil = new PDFutil();
        //一级标题
        table.addCell(pdFutil.getCell("作业区域生态适宜报告", pdFutil.getFont(Param.tittleFontSize, true), Param.tittleSzls, Param.tittleHigh, 1));
        table.addCell(pdFutil.getCell("生态指数条件", pdFutil.getFont(Param.tittleFontSize, true), Param.tittleSzls, Param.tittleHigh, 0));

        //生成key
        table.addCell(pdFutil.getCell("指标代码", pdFutil.getFont(Param.clofontSize, true), Param.cloSzls, Param.cloHigh, 0));
        table.addCell(pdFutil.getCell("指标名称", pdFutil.getFont(Param.clofontSize, true), Param.cloSzls, Param.cloHigh, 0));
        table.addCell(pdFutil.getCell("指数值", pdFutil.getFont(Param.clofontSize, true), Param.cloSzls, Param.cloHigh, 0));
        table.addCell(pdFutil.getCell("生态分类", pdFutil.getFont(Param.clofontSize, true), Param.cloSzls, Param.cloHigh, 0));
        List<Stzbtj> stzbtjs = pdfProperties.getStzbtj();
        stzbtjs.forEach(stzbtj -> {
            table.addCell(pdFutil.getCell(stzbtj.getZbdm(), pdFutil.getFont(Param.clofontSize, false), Param.cloSzls, Param.cloHigh, 0));
            table.addCell(pdFutil.getCell(stzbtj.getZbmc(), pdFutil.getFont(Param.clofontSize, false), Param.cloSzls, Param.cloHigh, 0));
            table.addCell(pdFutil.getCell(stzbtj.getZsz(), pdFutil.getFont(Param.clofontSize, false), Param.cloSzls, Param.cloHigh, 0));
            table.addCell(pdFutil.getCell(stzbtj.getZbfl(), pdFutil.getFont(Param.clofontSize, false), Param.cloSzls, Param.cloHigh, 0));
        });
        table.addCell(pdFutil.getCell("同质化区域信息", pdFutil.getFont(Param.tittleFontSize, true), Param.tittleSzls, Param.tittleHigh, 0));
        table.addCell(pdFutil.getCell("区域行政区", pdFutil.getFont(Param.clofontSize, true), Param.cloSzls * 2, Param.cloHigh, 0));
        table.addCell(pdFutil.getCell("行政区代码", pdFutil.getFont(Param.clofontSize, true), Param.cloSzls, Param.cloHigh, 0));
        table.addCell(pdFutil.getCell("面积(平方米)", pdFutil.getFont(Param.clofontSize, true), Param.cloSzls, Param.cloHigh, 0));
        List<TzhqytxDTO> tzhqytxDTOs = pdfProperties.getTzhqy();
        tzhqytxDTOs.forEach(tzhqytxDTO -> {
            table.addCell(pdFutil.getCell(tzhqytxDTO.getXzqmc(), pdFutil.getFont(Param.clofontSize, false), Param.cloSzls * 2, Param.cloHigh, 0));
            table.addCell(pdFutil.getCell(tzhqytxDTO.getXzqdm(), pdFutil.getFont(Param.clofontSize, false), Param.cloSzls, Param.cloHigh, 0));
            table.addCell(pdFutil.getCell(tzhqytxDTO.getMj(), pdFutil.getFont(Param.clofontSize, false), Param.cloSzls, Param.cloHigh, 0));
        });
        return table;
    }
}
